package com.eastfair.venuebooking.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venuebooking.dto.ExhibitionSpaceDTO;
import com.eastfair.venuebooking.dto.ExhibitionSpacePageQuery;
import com.eastfair.venuebooking.dto.ExhibitionSpaceSaveDTO;
import com.eastfair.venuebooking.dto.ExhibitionSpaceUpdateDTO;
import com.eastfair.venuebooking.entity.ExhibitionSpace;
import com.eastfair.venuebooking.service.ExhibitionSpaceService;
import com.eastfair.venuebooking.vo.ExhibitionSpaceVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 展会预定场地表
 * </p>
 *
 * @author gj
 * @date 2022-07-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/exhibitionSpace")
@Api(value = "ExhibitionSpace", tags = "展会预定场地表")
@PreAuth(replace = "venuebooking:exhibitionSpace:")
public class ExhibitionSpaceController extends SuperController<ExhibitionSpaceService, Long, ExhibitionSpace, ExhibitionSpacePageQuery, ExhibitionSpaceSaveDTO, ExhibitionSpaceUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ExhibitionSpaceService exhibitionSpaceService;

    @Override
    public void handlerResult(IPage<ExhibitionSpace> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ExhibitionSpace> exhibitionSpaceList = list.stream().map((map) -> ExhibitionSpace.builder().build()).collect(Collectors.toList());

        return R.success(baseService.saveBatch(exhibitionSpaceList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ExhibitionSpacePageQuery> params) {
        ExhibitionSpacePageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 展会ID查询展会场地
     *
     * @param exhibitionId the exhibition id
     * @return the r
     */
    @GetMapping("/exhibition/{exhibitionId}")
    public R<List<ExhibitionSpaceVO>> queryByExhibitionId(@PathVariable("exhibitionId") Long exhibitionId) {
        log.info("queryExhibitionSpace - 展会ID查询展会场地, exhibitionId={}", exhibitionId);
        try {
            ExhibitionSpaceDTO query = new ExhibitionSpaceDTO();
            query.setExhibitionId(exhibitionId);
            return R.successDef(exhibitionSpaceService.listExhibitionSpaceVOs(query));
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
